/*------------------------------------------------------------------------------

Fig A11 Bandwidth sensitivity, teacher variables I
							
------------------------------------------------------------------------------*/


global replica_dir="<add path to your working directory>"
global data_dir="$replica_dir/data"
global output_dir="$replica_dir/output"


use "$data_dir/tlvl_fin.dta", clear  

 
global depvars "horas_contrato  tea_classhrs" 
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"
 
								 
foreach yvar of global depvars {
	foreach i in 25 10{

		rdbwselect `yvar'_tp1 st_ind`i'  , all  c(0) p(2) kernel(uni) masspoints(off) vce(cluster rbd)

		gen bw_mse_`yvar'_`i'=e(h_mserd)
		gen bw_cer_`yvar'_`i'=e(h_cerrd)

		rdbwselect `yvar'_tp1 st_ind`i' if municip==0 , all   c(0) p(2) kernel(uni) masspoints(off) vce(cluster rbd)

		gen bw_mse_`yvar'_`i'_pv=e(h_mserd)
		gen bw_cer_`yvar'_`i'_pv=e(h_cerrd)

		rdbwselect `yvar'_tp1 st_ind`i' if municip==1 , all  c(0) p(2) kernel(uni) masspoints(off) vce(cluster rbd)

		gen bw_mse_`yvar'_`i'_mv=e(h_mserd)
		gen bw_cer_`yvar'_`i'_mv=e(h_cerrd)

}
}

foreach i in 25 10{
	foreach y of global depvars {

	replace bw_cer_`y'_`i'=round(bw_cer_`y'_`i',0.01)
	replace bw_mse_`y'_`i'=round(bw_mse_`y'_`i',0.01)

}
}


la var horas_contrato_tp1 	"Contract hours"
la var tea_classhrs_tp1  	"Classroom teaching hours"
 
la var treat25 "Full vs. Partial Bonus"
la var treat10 "Partial vs. No Bonus"

la var st_ind25 "100% vs. 60%"
la var st_ind10 "60% vs. none"

save "$data_dir/temp_tlvl_withbw_noindepvars.dta", replace
				
				
				
/*------------------------------------------------------------------------------	

	Regression RD - all

*/



use "$data_dir/temp_tlvl_withbw_noindepvars.dta", clear


global depvars 	 "horas_contrato  tea_classhrs"
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"
	
foreach i in 25 10{

local label_`i': variable label st_ind`i'
	
foreach y of global depvars {
	
		local label_`y': variable label `y'_tp1

		matrix define R = J(6, 9, .) // ( #of bw's being tested , #of statistics, missing )


		sum bw_cer_`y'_`i'
		local cer=round(r(min),0.1)  
		di `cer'

		sum bw_mse_`y'_`i'
		local mse=round(r(min),0.1)  
		di `mse'
		
		local cer2=`cer'*2
		local mse2=`mse'*2
		
		local r = 1
		
		foreach bw in `cer'  `mse' `cer2'  `mse2'   1.5  3  {
				
			reg `y'_tp1 treat`i' st_ind`i' st_ind`i'_sq inter`i' inter`i'_sq  ${indepvars} `y'_tm1 if abs(st_ind`i')<=`bw', vce(cluster rbd)

			matrix list r(table)
			matrix beta=r(table)

			matrix R[`r', 1] =`bw'  
			matrix R[`r', 4] = beta[1,1]  
			matrix R[`r', 5] = beta[2,1]  
			matrix R[`r', 6] = beta[4,1]  
			matrix R[`r', 7] = beta[5,1]  
			matrix R[`r', 8] = beta[6,1]  
		 
			matrix list R
		 
			local r = `r' + 1 
		}

clear
svmat R

twoway (rcap R7 R8 R1, lcolor(navy)) ///
	(scatter R4 R1, mcolor(cranberry) yline(0, lcolor(black) lpattern(dash))), ///
	graphregion(color(white)) /// 
	xlabel( `cer'   "CER" `mse'  "MSE"  1.5 "1.5"  0 "0" `cer2' "2*CER"  `mse2' "2*MSE" 3 "3" , angle(45))  /// 
	ylabel(, angle(horizontal)) legend(off) xtitle(" ") /*xtitle("Bandwidth") ytitle("RD estimate")*/  /// 
	title(" `label_`y'', All Schools ", bmargin(0)) subtitle(" `label_`i'' ", bmargin(0))   

graph export "$output_dir/graph_bwsensitivity_t_`y'_tp1_`i'.png", as(png) replace


use "$data_dir/temp_tlvl_withbw_noindepvars.dta", clear
global depvars 	 "horas_contrato  tea_classhrs"
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

}
}

				
				
				
/*------------------------------------------------------------------------------	

	Regression RD - private
*/


use "$data_dir/temp_tlvl_withbw_noindepvars.dta", clear

global depvars 	 "horas_contrato tea_classhrs"
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

cd "$output_dir"

keep if municip==0
	
foreach i in 25 10{

local label_`i': variable label st_ind`i'

foreach y of global depvars {
	
		local label_`y': variable label `y'_tp1

		matrix define R = J(6, 9, .) // ( #of bw's being tested , #of statistics, missing )


		sum bw_cer_`y'_`i'
		local cer=round(r(min),0.1) // r(min) // 
		di `cer'

		sum bw_mse_`y'_`i'
		local mse=round(r(min),0.1) // r(min) // round(r(min),0.1)
		di `mse'
		
		local cer2=`cer'*2
		local mse2=`mse'*2
		
		local r = 1
		
		foreach bw in `cer'  `mse' `cer2'  `mse2'   1.5  3  {
				
			reg `y'_tp1 treat`i' st_ind`i' st_ind`i'_sq inter`i' inter`i'_sq  ${indepvars} `y'_tm1 if abs(st_ind`i')<=`bw', vce(cluster rbd)

			matrix list r(table)
			matrix beta=r(table)

			matrix R[`r', 1] =`bw'  
			matrix R[`r', 4] = beta[1,1]  
			matrix R[`r', 5] = beta[2,1]  
			matrix R[`r', 6] = beta[4,1] 
			matrix R[`r', 7] = beta[5,1]  
			matrix R[`r', 8] = beta[6,1]  
		 
			matrix list R
		 
			local r = `r' + 1 
		}

clear
svmat R

twoway (rcap R7 R8 R1, lcolor(navy)) ///
	(scatter R4 R1, mcolor(cranberry) yline(0, lcolor(black) lpattern(dash))), ///
	graphregion(color(white)) /// 
	xlabel( `cer'   "CER" `mse'  "MSE"  1.5 "1.5"  0 "0" `cer2' "2*CER"  `mse2' "2*MSE" 3 "3" , angle(45))  /// 
	ylabel(, angle(horizontal)) legend(off) xtitle(" ") /*xtitle("Bandwidth") ytitle("RD estimate")*/  /// 
	title(" `label_`y'', Private Schools ", bmargin(0)) subtitle(" `label_`i'' ", bmargin(0))   

graph export "$output_dir/graph_bwsensitivity_t_`y'_tp1_`i'_pv.png", as(png) replace


use "$data_dir/temp_tlvl_withbw_noindepvars.dta", clear

global depvars 	 "horas_contrato  tea_classhrs"
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

keep if municip==0


}
}






/*------------------------------------------------------------------------------	

	Regression RD - municipal
*/


use "$data_dir/temp_tlvl_withbw_noindepvars.dta", clear

global depvars 	 "horas_contrato tea_classhrs"
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

keep if municip==1
	
foreach i in 25 10{

local label_`i': variable label st_ind`i'

foreach y of global depvars {
	
		local label_`y': variable label `y'_tp1

		matrix define R = J(6, 9, .) // ( #of bw's being tested , #of statistics, missing )


		sum bw_cer_`y'_`i'
		local cer=round(r(min),0.1)  
		di `cer'

		sum bw_mse_`y'_`i'
		local mse=round(r(min),0.1)  
		di `mse'
		
		local cer2=`cer'*2
		local mse2=`mse'*2
		
		local r = 1
		
		foreach bw in `cer'  `mse' `cer2'  `mse2'   1.5  3 {
				
			reg `y'_tp1 treat`i' st_ind`i' st_ind`i'_sq inter`i' inter`i'_sq  ${indepvars} `y'_tm1 if abs(st_ind`i')<=`bw', vce(cluster rbd)

			matrix list r(table)
			matrix beta=r(table)

			matrix R[`r', 1] =`bw'  
			matrix R[`r', 4] = beta[1,1]  
			matrix R[`r', 5] = beta[2,1]  
			matrix R[`r', 6] = beta[4,1]  
			matrix R[`r', 7] = beta[5,1] 
			matrix R[`r', 8] = beta[6,1]  
		 
			matrix list R
		 
			local r = `r' + 1 
		}

clear
svmat R

twoway (rcap R7 R8 R1, lcolor(navy)) ///
	(scatter R4 R1, mcolor(cranberry) yline(0, lcolor(black) lpattern(dash))), ///
	graphregion(color(white)) /// 
	xlabel( `cer'   "CER" `mse'  "MSE"  1.5 "1.5"  0 "0" `cer2' "2*CER"  `mse2' "2*MSE" 3 "3", angle(45))  /// 
	ylabel(, angle(horizontal)) legend(off) xtitle(" ") /*xtitle("Bandwidth") ytitle("RD estimate")*/  /// 
	title(" `label_`y'', Municipal Schools ", bmargin(0)) subtitle(" `label_`i'' ", bmargin(0))   
	
graph export "$output_dir/graph_bwsensitivity_t_`y'_tp1_`i'_mv.png", as(png) replace


use "$data_dir/temp_tlvl_withbw_noindepvars.dta", clear

global depvars 	 "horas_contrato tea_classhrs"
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

keep if municip==1

}
}

erase "$data_dir/temp_tlvl_withbw_noindepvars.dta"




